﻿using System;
using System.Threading;
using GC_FFW.Common.Enums;
using GC_FFW.Common.Helpers;

namespace GC_FFW.ForecastWriterClient.Helpers
{
	public static class SafeOperationsManager
	{
		public static void SafeExecute(Action code)
		{
			if (null == code) return;

			try
			{
				code();
			}
			catch (ThreadAbortException)
			{
				ApplicationLogger.Logger.LoggingMessage("Start Aborting Thread.", LoggerMessageType.Info);
			}
			catch (Exception ex)
			{
				ApplicationLogger.Logger.LoggingExceptionMessage(ex);
			}
		}

		public static bool SafeExecute<TException>(Action code, Action<TException> exCode)
			where TException : Exception
		{
			if (code == null) return false;

			try
			{
				code();
				return true;
			}
			catch (ThreadAbortException)
			{
				return true;
			}
			catch (TException ex)
			{
				if (exCode != null)
				{
					exCode(ex);
				}
				ApplicationLogger.Logger.LoggingExceptionMessage(ex);
			}
			catch (Exception ex)
			{
				ApplicationLogger.Logger.LoggingExceptionMessage(ex);
			}

			return false;
		}
	}
}
